class ListNode{
    public ListNode prev;
    public ListNode next;
    public int val;

    public ListNode(int val){
        this.val = val;
    }

}

public class MyLinckedList {
    public ListNode last;
    ListNode head = new ListNode(-1);

    //打印链表
    public void display(){
        ListNode cur = this.head.next;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head.next;
        while(cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head.next;
        if(cur == null){
            return false;
        }

        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head.next;

        if(cur == null) {
            head.next = node;
            node.next = cur;
            node.prev = head;
        }else{
            head.next = node;
            node.next = cur;
            cur.prev = node;
            node.prev = head;
        }

    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head.next == null){
            this.last = head;
        }
        this.last.next = node;
        node.prev = this.last;
        this.last = node;

    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int len,int data){
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        ListNode curNext = cur.next;

        while(len != 0){
            cur = curNext;
            curNext = cur.next;
            if(curNext == null){
                cur.next = node;
                node.prev = cur;
                return;
            }
            len--;
        }
        cur.next = node;
        node.next = curNext;
        curNext.prev = node;
        node.prev = cur;
        return ;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = this.head.next;
        if(cur == null){
            return;
        }
        while(cur != null){
            if(cur.val == key){
                if(head.next.val == key){
                    head.next = cur.next;
                    cur.next.prev = null;
                    return;
                }else{
                    if(this.last.val == key){
                        cur.prev.next = null;
                        last = cur.prev;
                        return;
                    }else {
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    return;
                    }
                }
            }else{
                cur = cur.next;
            }
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = this.head.next;
        if(cur == null){
            return;
        }
        while(cur != null){
            if(cur.val == key){
                if(head.next.val == key){
                    head.next = cur.next;
                    cur.next.prev = null;
                }else{
                    if(this.last.val == key){
                        cur.prev.next = null;
                        last = cur.prev;
                    }else {
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
            }else{
                cur = cur.next;
            }
        }
    }

    //清空链表
    public void clear(){
        while(this.head != null){
            ListNode curNext = this.head.next;
            this.head.prev = null;
            this.head.prev = null;
            this.head.next = null;
            this.head = curNext;
        }
        this.last = null;
    }

}
